/*******************************************************************************
* Copyright (c) 2010, 2018 西安秦晔信息科技有限公司
* Licensed under the Apache License, Version 2.0 (the "License");
*    you may not use this file except in compliance with the License.
*    You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*    Unless required by applicable law or agreed to in writing, software
*    distributed under the License is distributed on an "AS IS" BASIS,
*    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*    See the License for the specific language governing permissions and
*    limitations under the License.
*******************************************************************************/
package com.qinyeit.serviceapp.service.dishes.impl;

import com.qinyeit.serviceapp.entity.DishesStore;
import com.qinyeit.serviceapp.entity.enums.Industries;
import com.qinyeit.serviceapp.exception.BusinessRunTimeException;
import com.qinyeit.serviceapp.utils.Lang;
import com.qinyeit.serviceapp.utils.LocalUtils;
import com.qinyetech.springstage.core.date.DateUtils;
import com.qinyetech.springstage.core.lang.QyMap;
import com.qinyetech.springstage.core.repository.jdbc.PageNamedParameterJdbcTemplate;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qinyetech.springstage.core.service.BaseService;
import com.qinyeit.serviceapp.entity.DishesCategory;
import com.qinyeit.serviceapp.repository.dishes.DishesCategoryRepository;
import com.qinyeit.serviceapp.service.dishes.DishesCategoryService;

import java.util.List;
import java.util.Map;

/** 
 * ClassName: DishesCategoryServiceImpl <br/> 
 * Function: 菜品分类 业务实现 <br/> 
 * date: 2018-11-23 11:05:47 <br/> 
 * 
 * @author liuwenjin
 * @version 1.0
 * @since JDK 1.8
 */
@Service
@Transactional
public class DishesCategoryServiceImpl extends BaseService<DishesCategory, Long> implements DishesCategoryService {


	@Autowired
	private DishesCategoryRepository dishesCategoryRepository;
	@Autowired
	private PageNamedParameterJdbcTemplate jdbcTemplate;


	@Override
	public void createDefault(DishesStore dishesStore, Long merchantGroupId) {
		DishesCategory category = new DishesCategory();
		if(dishesStore.getIndustry()== Industries.LS || dishesStore.getIndustry()==Industries.SX){
			category.setName("组合商品");
		}else {
			category.setName("套餐");
		}
		category.setNo("00000");
		category.setStoreId(dishesStore.getId());
		category.setWeight(0.0);
		category.setMerchantGroupId(merchantGroupId);
		category.setUuid(LocalUtils.getUUID());
		category.setUpdateTimestamp(DateUtils.current());
		this.save(category);
	}

	@Override
	public String getNextNo(String defNo,Long merchantGroupId) {
		String sql = "SELECT * FROM dishes_categorys WHERE merchant_group_id=:merchantGroupId and is_deleted=false AND no != '00000' ORDER BY id DESC LIMIT 1";
		QyMap param = QyMap.map("merchantGroupId",merchantGroupId);
		List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql, param);
		if(CollectionUtils.isNotEmpty(maps)){
			Map<String, Object> map = maps.get(0);
			if(map.containsKey("no")){
				String lastNo = map.get("no").toString();
				if(lastNo.length()>2){
					lastNo = lastNo.substring(lastNo.length()-2);
				}
				return Lang.getNextNo(lastNo);
			}
		}
		return defNo;
	}
}